package cn.iocoder.yudao.module.member.service.userrelation;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.CheckUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.member.controller.admin.userrelation.vo.UserRelationCreateReqVO;
import cn.iocoder.yudao.module.member.controller.admin.userrelation.vo.UserRelationExportReqVO;
import cn.iocoder.yudao.module.member.controller.admin.userrelation.vo.UserRelationPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.userrelation.vo.UserRelationUpdateReqVO;
import cn.iocoder.yudao.module.member.convert.userrelation.UserRelationConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.userrelation.UserRelationDO;
import cn.iocoder.yudao.module.member.dal.mysql.userrelation.UserRelationMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.USER_RELATION_NOT_EXISTS;

/**
 * 户关系 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class UserRelationServiceImpl implements UserRelationService {

    @Resource
    private UserRelationMapper userRelationMapper;

    @Override
    public Long createUserRelation(UserRelationCreateReqVO createReqVO) {
        // 插入
        UserRelationDO userRelation = UserRelationConvert.INSTANCE.convert(createReqVO);
        userRelationMapper.insert(userRelation);
        // 返回
        return userRelation.getId();
    }

    @Override
    public void updateUserRelation(UserRelationUpdateReqVO updateReqVO) {
        // 校验存在
        validateUserRelationExists(updateReqVO.getId());
        // 更新
        UserRelationDO updateObj = UserRelationConvert.INSTANCE.convert(updateReqVO);
        userRelationMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserRelation(Long id) {
        // 校验存在
        validateUserRelationExists(id);
        // 删除
        userRelationMapper.deleteById(id);
    }

    private void validateUserRelationExists(Long id) {
        if (userRelationMapper.selectById(id) == null) {
            throw exception(USER_RELATION_NOT_EXISTS);
        }
    }

    @Override
    public UserRelationDO getUserRelation(Long id) {
        return userRelationMapper.selectById(id);
    }

    @Override
    public List<UserRelationDO> getUserRelationList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return userRelationMapper.selectBatchIds(ids);
    }

    @Override
    public List<UserRelationDO> getUserRelationList(Long memberUserId) {
        if (CheckUtils.isEmpty(memberUserId)) {
            return ListUtil.empty();
        }
        LambdaQueryWrapperX<UserRelationDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(UserRelationDO::getMemberUserId,memberUserId);
        queryWrapper.eq(UserRelationDO::getDeleted,0);
         return userRelationMapper.selectList(queryWrapper);
    }

    @Override
    public List<UserRelationDO> getUserRelations(Long crmMemberId) {
        if (CheckUtils.isEmpty(crmMemberId)) {
            return ListUtil.empty();
        }
        LambdaQueryWrapperX<UserRelationDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(UserRelationDO::getCrmMemberUserId,crmMemberId);
        queryWrapper.eq(UserRelationDO::getDeleted,0);
        return userRelationMapper.selectList(queryWrapper);
    }

    @Override
    public List<UserRelationDO> getUserRelationList(Long memberUserId,Long crmMemberUserId) {
        LambdaQueryWrapperX<UserRelationDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(UserRelationDO::getMemberUserId,memberUserId);
        queryWrapper.eq(UserRelationDO::getCrmMemberUserId,crmMemberUserId);
        queryWrapper.eq(UserRelationDO::getDeleted,0);
        return userRelationMapper.selectList(queryWrapper);
    }

    @Override
    public PageResult<UserRelationDO> getUserRelationPage(UserRelationPageReqVO pageReqVO) {
        return userRelationMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UserRelationDO> getUserRelationList(UserRelationExportReqVO exportReqVO) {
        return userRelationMapper.selectList(exportReqVO);
    }

    @Override
    public UserRelationDO getUserRelationOne(Long crmMemberUserId) {
        LambdaQueryWrapperX<UserRelationDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(UserRelationDO::getCrmMemberUserId,crmMemberUserId);
        queryWrapper.eq(UserRelationDO::getStatus,0);
        queryWrapper.eq(UserRelationDO::getRelation,0);
        return userRelationMapper.selectOne(queryWrapper);
    }

    @Override
    public Long getUserRelationCount(Long memberUserId) {
        LambdaQueryWrapperX<UserRelationDO> queryWrapper = new LambdaQueryWrapperX<>();
        queryWrapper.eq(UserRelationDO::getMemberUserId,memberUserId);
        queryWrapper.eq(UserRelationDO::getStatus,0);
        return userRelationMapper.selectCount(queryWrapper);
    }

}
